/**
 * @Author 韦建韩
 * @Date 2022/5/20
 */
package com.example.devops.web.controller.applicationAdmin;

import com.example.devops.entity.common.Result;
import com.example.devops.entity.pojo.LogFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("logFile")
@CrossOrigin
public class LogFileController {
    Logger log = LoggerFactory.getLogger(LogFileController.class);

    @RequestMapping("getLogFile")
    public Result getLogFile() {
//        String path = "E:\\log";
        String path = "/devops/target/logFile";
        File parent = new File(path);
        File[] files = parent.listFiles();
        List<LogFile> fileList = new ArrayList<>();
        for (File file : files) {
            if (file.isDirectory()) {
                File[] files1 = file.listFiles();
                for (File file1 : files1) {
                    if (file1.isFile()) {
                        String fileAddr = file1.getPath();
                        String fileName = file1.getName();
                        LogFile logFile = new LogFile();
                        logFile.setFile(fileName);
                        logFile.setAddr(fileAddr);
                        fileList.add(logFile);
                    }
                }
            }
            else{
                String fileAddr = file.getPath();
                String fileName = file.getName();
                LogFile logFile = new LogFile();
                logFile.setFile(fileName);
                logFile.setAddr(fileAddr);
                fileList.add(logFile);
            }
        }
        return Result.doCreate(fileList);
    }

    @RequestMapping("loadLogFile")
    public void loadLogFile(String path , HttpServletResponse response) throws UnsupportedEncodingException {
        String encode = URLDecoder.decode(path, "UTF-8");
        File file = new File(encode);
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            //文件是否存在
            if (file.exists()) {
                //设置响应
                response.setContentType("application/octet-stream;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                os = response.getOutputStream();
                bis = new BufferedInputStream(new FileInputStream(file));
                while(bis.read(buffer) != -1){
                    os.write(buffer);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(bis != null) {
                    bis.close();
                }
                if(os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


}
